{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Largest Unique Number"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: largestUniqueNumber"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #最大唯一数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组&nbsp;<code>A</code>，请找出并返回在该数组中仅出现一次的最大整数。</p>\n",
    "\n",
    "<p>如果不存在这个只出现一次的整数，则返回 -1。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>[5,7,3,9,4,9,8,3,1]\n",
    "<strong>输出：</strong>8\n",
    "<strong>解释： </strong>\n",
    "数组中最大的整数是 9，但它在数组中重复出现了。而第二大的整数是 8，它只出现了一次，所以答案是 8。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>[9,9,8,8]\n",
    "<strong>输出：</strong>-1\n",
    "<strong>解释： </strong>\n",
    "数组中不存在仅出现一次的整数。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ol>\n",
    "\t<li><code>1 &lt;= A.length &lt;= 2000</code></li>\n",
    "\t<li><code>0 &lt;= A[i] &lt;= 1000</code></li>\n",
    "</ol>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [largest-unique-number](https://leetcode.cn/problems/largest-unique-number/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [largest-unique-number](https://leetcode.cn/problems/largest-unique-number/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[5,7,3,9,4,9,8,3,1]', '[9,9,8,8]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        count: Dict[int, int] = Counter(nums)\n",
    "        once: Set[int] = set(n for n, c in count.items() if c == 1)\n",
    "        return (-1 if len(once) == 0 else max(once))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        c = Counter(nums)\n",
    "\n",
    "        nums.sort()\n",
    "        nums.reverse()\n",
    "\n",
    "        for i in nums:\n",
    "            if c[i] == 1:\n",
    "                return i\n",
    "        \n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        res = -1\n",
    "        for k, v in Counter(nums).items():\n",
    "            if v == 1:\n",
    "                res = max(res, k)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        m = collections.defaultdict(int)\n",
    "        for num in nums:\n",
    "            m[num] +=1\n",
    "        \n",
    "        mx = -1\n",
    "        for k, v in m.items():\n",
    "            if v == 1 and k > mx:\n",
    "                mx = k \n",
    "        return mx "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, A: List[int]) -> int:\n",
    "        m = collections.defaultdict(int)\n",
    "        for num in A:\n",
    "            m[num] += 1\n",
    "        \n",
    "        mx = -1\n",
    "        for k, v in m.items():\n",
    "            if v == 1 and k > mx:\n",
    "                mx = k\n",
    "        return mx\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        c = Counter(nums)\n",
    "\n",
    "        nums.sort()\n",
    "        nums.reverse()\n",
    "\n",
    "        for i in nums:\n",
    "            if c[i] == 1:\n",
    "                return i\n",
    "        \n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, A: List[int]) -> int:\n",
    "        m = collections.defaultdict(int)\n",
    "        for num in A:\n",
    "            m[num] += 1\n",
    "        \n",
    "        mx = -1\n",
    "        for k, v in m.items():\n",
    "            if v == 1 and k > mx:\n",
    "                mx = k\n",
    "        return mx\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "#Python3 字典模拟\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        dic = Counter(nums)#k,v，数值，频次\n",
    "        res = -1\n",
    "        for k,v in dic.items():\n",
    "            if v == 1:#唯一数，频次为一\n",
    "                res = max(res,k)#最大唯一数\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        cnt = collections.Counter(nums)\n",
    "        res = -1\n",
    "        for num in nums:\n",
    "            if cnt[num]==1 and num>res:res=num\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse=True)\n",
    "        if len(nums) == 1:\n",
    "            return nums[0]\n",
    "        for i in range(len(nums)):\n",
    "            if i == 0:\n",
    "                if nums[i] != nums[i+1]:\n",
    "                    return nums[i]\n",
    "            elif i >0 and i <= len(nums)-2:\n",
    "                if nums[i] != nums[i+1] and  nums[i] != nums[i-1]:\n",
    "                    return nums[i]\n",
    "            else:\n",
    "                if nums[i] != nums[i-1]:\n",
    "                    return nums[i]\n",
    "\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        nums.sort(reverse=True)\n",
    "        for i in range(n):\n",
    "            if (i == 0 or nums[i - 1] != nums[i]) and \\\n",
    "            (i == n - 1 or nums[i] != nums[i + 1]):\n",
    "                return nums[i]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        c = Counter(nums)\n",
    "\n",
    "        max1 = -1\n",
    "        for i in c:\n",
    "            if c[i] == 1:\n",
    "                max1 = max(max1, i)\n",
    "        return max(max1, -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        hashmap={}\n",
    "        for i in nums:\n",
    "            hashmap[i]=hashmap.get(i,0)+1\n",
    "        for a,b in hashmap.copy().items():\n",
    "            if b>1:\n",
    "                del hashmap[a]\n",
    "        if hashmap:\n",
    "            return max(hashmap.keys())\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        c = Counter(nums)\n",
    "\n",
    "        nums.sort()\n",
    "        # nums.reverse()\n",
    "\n",
    "        for i in nums[::-1]:\n",
    "            if c[i] == 1:\n",
    "                return i\n",
    "        \n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        c = Counter(nums)\n",
    "\n",
    "        nums.sort()\n",
    "        # nums.reverse()\n",
    "\n",
    "        for i in nums[::-1]:\n",
    "            if c[i] == 1:\n",
    "                return i\n",
    "        \n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        return max((key  for key, val in Counter(nums).items() if val == 1), default=-1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        hashmap = {}\n",
    "        max_n = 0\n",
    "        for item in nums:\n",
    "            if item in hashmap:\n",
    "                hashmap[item]+=1\n",
    "            else:\n",
    "                hashmap[item]=1\n",
    "            \n",
    "        for item in hashmap:\n",
    "            if item > max_n and hashmap[item]==1:\n",
    "                max_n = item\n",
    "        if max_n==0:\n",
    "            return -1\n",
    "        return max_n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        counter = Counter(nums)\n",
    "        res = -1\n",
    "        for num in counter:\n",
    "            if counter[num] == 1:\n",
    "                res = max(num, res)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        c = collections.Counter(nums)\n",
    "\n",
    "        ret = -1\n",
    "        for k, v in c.items():\n",
    "            if v == 1 and k > ret:\n",
    "                ret = k\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        d = collections.Counter(nums)\n",
    "        ans = -1\n",
    "        for k, v in d.items():\n",
    "            if v == 1:\n",
    "                ans = max(ans, k)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        return max([k for k, v in collections.Counter(nums).items() if v == 1], default= -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        nums.sort(reverse=True)\n",
    "        for i in range(n):\n",
    "            if (i == 0 or nums[i - 1] != nums[i]) and \\\n",
    "            (i == n - 1 or nums[i] != nums[i + 1]):\n",
    "                return nums[i]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse=True)\n",
    "        return next((x for x, g in groupby(nums) if next(g) != next(g, -1)), -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        hs = defaultdict(int)\n",
    "        for i in nums:\n",
    "            hs[i] += 1\n",
    "\n",
    "        order = sorted(list(hs.keys()),reverse = True)\n",
    "        for j in order:\n",
    "            if hs[j] == 1:\n",
    "                return j\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        arr = sorted(Counter(nums).items(), key=lambda x:-x[0])\n",
    "        for k, v in arr:\n",
    "            if v == 1:\n",
    "                return k\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        if len(nums) == 1:\n",
    "            return nums[0]\n",
    "        nums = sorted(nums,reverse=True)\n",
    "        dup = -1\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i] != dup and nums[i] != nums[i+1]:\n",
    "                return nums[i]\n",
    "            else:\n",
    "                dup = nums[i]\n",
    "        if nums[-1] != nums[-2]:\n",
    "            return nums[-1]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber1(self, A: List[int]) -> int:\n",
    "        m = collections.defaultdict(int)\n",
    "        for num in A:\n",
    "            m[num] += 1\n",
    "        \n",
    "        mx = -1\n",
    "        for k, v in m.items():\n",
    "            if v == 1 and k > mx:\n",
    "                mx = k\n",
    "        return mx\n",
    "\n",
    "    # 计数排序\n",
    "    def largestUniqueNumber(self, A: List[int]) -> int:\n",
    "        r = [0] * 1001\n",
    "        for num in A:\n",
    "            r[num] += 1\n",
    "        for i in range(1000, -1, -1):\n",
    "            if r[i] == 1:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        from collections import Counter\n",
    "        sub_nums = [k for k,v in Counter(nums).items() if v ==1]\n",
    "        return max(sub_nums) if sub_nums else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        \n",
    "        d=defaultdict(int)\n",
    "        for num in nums:\n",
    "            d[num]+=1\n",
    "        \n",
    "        k=list(sorted(d.keys()))\n",
    "\n",
    "        for num in reversed(k):\n",
    "            if d[num]==1:\n",
    "                return num \n",
    "\n",
    "        return -1            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        c = Counter(nums)\n",
    "\n",
    "        nums.sort()\n",
    "        nums.reverse()\n",
    "\n",
    "        for i in nums:\n",
    "            if c[i] == 1:\n",
    "                return i\n",
    "        \n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse = True)\n",
    "        if len(nums) == 1:\n",
    "            return nums[0]\n",
    "        for i in range(len(nums)):\n",
    "            if i == 0:\n",
    "                if nums[0] == nums[1]:\n",
    "                    continue\n",
    "                else:\n",
    "                    return nums[i]\n",
    "            elif i == len(nums)-1:\n",
    "                if nums[-1] == nums[-2]:\n",
    "                    return -1\n",
    "                else:\n",
    "                    return nums[-1]\n",
    "            else:\n",
    "                if nums[i] != nums[i-1] and nums[i] != nums[i+1]:\n",
    "                    return nums[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        c = Counter(nums)\n",
    "\n",
    "        max1 = -1\n",
    "        for i in c:\n",
    "            if c[i] == 1:\n",
    "                max1 = max(max1, i)\n",
    "        return max(max1, -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        cnt = Counter(nums)\n",
    "        for num in cnt:\n",
    "            if cnt[num] == 1:\n",
    "                ans = max(ans,num)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        count = {}\n",
    "        max_unique = -1\n",
    "        for num in nums:\n",
    "            count[num] = count.get(num, 0) + 1\n",
    "        for count_num in count:\n",
    "            if count[count_num] == 1:\n",
    "                max_unique = max(max_unique, count_num)\n",
    "        return max_unique"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, A: List[int]) -> int:\n",
    "        m = collections.defaultdict(int)\n",
    "        for num in A:\n",
    "            m[num] += 1\n",
    "        \n",
    "        mx = -1\n",
    "        for k, v in m.items():\n",
    "            if v == 1 and k > mx:\n",
    "                mx = k\n",
    "        return mx\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        c = Counter(nums)\n",
    "\n",
    "        max1 = -1\n",
    "        for i in c:\n",
    "            if c[i] == 1:\n",
    "                max1 = max(max1, i)\n",
    "        return max(max1, -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        \n",
    "        d=Counter(nums)\n",
    "        \n",
    "        k=list(sorted(d.keys()))\n",
    "\n",
    "        for num in reversed(k):\n",
    "            if d[num]==1:\n",
    "                return num \n",
    "\n",
    "        return -1            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        m = collections.defaultdict(int)\n",
    "        for i in nums:\n",
    "            m[i] += 1\n",
    "        mx = -1\n",
    "        for k,v in m.items():\n",
    "            if v == 1 and k > mx:\n",
    "                mx = k\n",
    "        return mx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        dt= dict()\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] not in dt:\n",
    "                dt[nums[i]]=1\n",
    "            else: dt[nums[i]]+=1\n",
    "        m=-1\n",
    "        # print(dt)\n",
    "        for key in dt.keys():\n",
    "            if dt[key]==1:\n",
    "                m=max(m,key)\n",
    "        return m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        dic = collections.Counter(nums)\n",
    "        res = [k for k,v in dic.items() if v==1]\n",
    "\n",
    "        if not res:\n",
    "            return -1\n",
    "        else:\n",
    "            return max(res)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        c = Counter(nums)\n",
    "        for vv in c:\n",
    "            if c[vv] == 1 and vv > ans:\n",
    "                ans = vv\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, A: List[int]) -> int:\n",
    "        m = collections.defaultdict(int)\n",
    "        for num in A:\n",
    "            m[num] += 1\n",
    "        \n",
    "        mx = -1\n",
    "        for k, v in m.items():\n",
    "            if v == 1 and k > mx:\n",
    "                mx = k\n",
    "        return mx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        # 构建计数字典进行统计\n",
    "        #\n",
    "        count_dic = {}\n",
    "        for num in nums:\n",
    "            if num in count_dic:\n",
    "                count_dic[num]+=1\n",
    "            else:\n",
    "                count_dic[num] = 1\n",
    "        # # 对字典进行排序\n",
    "        # sorted_dict_by_key = dict(sorted(count_dic.items(), key=lambda item: item[0],reverse=True))\n",
    "\n",
    "        # # print(sorted_dict_by_key)\n",
    "        # for key,value in sorted_dict_by_key.items():\n",
    "        #     if value==1:\n",
    "        #         return key\n",
    "        # return -1\n",
    "\n",
    "        unique_value = [key for key,value in count_dic.items() if value==1]\n",
    "        if unique_value:\n",
    "            return max(unique_value)\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        max_v = -1\n",
    "        idx = []\n",
    "        while True:\n",
    "            for i in range(n):\n",
    "                if nums[i] > max_v:\n",
    "                    max_v = nums[i]\n",
    "                    idx = [i]\n",
    "                elif nums[i] == max_v:\n",
    "                    idx.append(i)\n",
    "            if len(idx) == 1:\n",
    "                return max_v\n",
    "            elif len(idx) == 0:\n",
    "                return -1\n",
    "            for i in idx:\n",
    "                nums[i] = 0\n",
    "            if max_v == 0:\n",
    "                return -1\n",
    "            max_v = 0\n",
    "            idx = []\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums, reverse=True)\n",
    "        for j in nums:\n",
    "            if nums.count(j)==1:\n",
    "                return j\n",
    "        return -1        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        record = collections.defaultdict(int)\n",
    "        for num in nums:\n",
    "            record[num] += 1\n",
    "        res = -1\n",
    "        for key in record:\n",
    "            if record[key] == 1:\n",
    "                res = max(res, key)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        # 构建计数字典进行统计\n",
    "        #\n",
    "        count_dic = {}\n",
    "        for num in nums:\n",
    "            if num in count_dic:\n",
    "                count_dic[num]+=1\n",
    "            else:\n",
    "                count_dic[num] = 1\n",
    "        # 对字典进行排序\n",
    "        sorted_dict_by_key = dict(sorted(count_dic.items(), key=lambda item: item[0],reverse=True))\n",
    "\n",
    "        # print(sorted_dict_by_key)\n",
    "        for key,value in sorted_dict_by_key.items():\n",
    "            if value==1:\n",
    "                return key\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        \n",
    "        d = defaultdict(int)\n",
    "\n",
    "        for num in nums:\n",
    "            d[num] += 1\n",
    "        \n",
    "        one_app_num = [k for k in d if d[k] == 1]\n",
    "        return max(one_app_num) if one_app_num else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        hashmap = {}\n",
    "        \n",
    "        for i in nums:\n",
    "            if i not in hashmap:\n",
    "                hashmap[i] = 1\n",
    "            else:\n",
    "                hashmap[i] += 1\n",
    "\n",
    "        res = 0\n",
    "        for i in hashmap:\n",
    "            if hashmap[i] == 1:\n",
    "                res = max(res, i)\n",
    "        if res == 0:\n",
    "            return -1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 排序 + 哈希\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        # # 哈希\n",
    "        # h_dict = dict()\n",
    "        # for num in nums:\n",
    "        #     h_dict[num] = h_dict.get(num, 0) + 1\n",
    "        # h_dict = {k: v for k, v in h_dict.items() if v == 1}\n",
    "        # return sorted(list(h_dict.keys()))[-1] if h_dict else -1\n",
    "\n",
    "        # 直接解法\n",
    "        nums.sort(reverse=True)\n",
    "        for num in nums:\n",
    "            if nums.count(num) == 1:\n",
    "                return num\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        \n",
    "        d=defaultdict(int)\n",
    "        for num in nums:\n",
    "            d[num]+=1\n",
    "        \n",
    "        k=list(sorted(d.keys()))\n",
    "\n",
    "        for num in reversed(k):\n",
    "            if d[num]==1:\n",
    "                return num \n",
    "\n",
    "        return -1            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        counter = collections.Counter(nums)\n",
    "        result = -1\n",
    "        for num in nums:\n",
    "            if num > result and counter[num] == 1:\n",
    "                result = num\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        ans, cnt = -1, Counter(nums)\n",
    "\n",
    "        for k, v in cnt.items():\n",
    "            if v == 1:\n",
    "                ans = max(ans, k)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        hashmap={}\n",
    "        for i in nums:\n",
    "            hashmap[i]=hashmap.get(i,0)+1\n",
    "        for a,b in hashmap.copy().items():\n",
    "            if b>1:\n",
    "                del hashmap[a]\n",
    "        if hashmap:\n",
    "            return max(hashmap.keys())\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 排序 + 哈希\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        # 哈希\n",
    "        h_dict = dict()\n",
    "        for num in nums:\n",
    "            h_dict[num] = h_dict.get(num, 0) + 1\n",
    "        h_dict = {k: v for k, v in h_dict.items() if v == 1}\n",
    "        return sorted(list(h_dict.keys()))[-1] if h_dict else -1\n",
    "\n",
    "        # 直接解法\n",
    "        nums.sort(reverse=True)\n",
    "        for num in nums:\n",
    "            if nums.count(num) == 1:\n",
    "                return num\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        from heapq import heappush, heappop\n",
    "        myPQ = []\n",
    "        memo = dict()\n",
    "        for num in nums:\n",
    "            heappush(myPQ,-num)\n",
    "            memo.setdefault(num, 0)\n",
    "            memo[num] += 1\n",
    "        while True:\n",
    "            if myPQ:\n",
    "                curEle = -heappop(myPQ)\n",
    "            else:\n",
    "                break\n",
    "            if memo[curEle] == 1:\n",
    "                return curEle\n",
    "        return -1\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        max_v = -1\n",
    "        idx = []\n",
    "        while True:\n",
    "            for i in range(n):\n",
    "                if nums[i] > max_v:\n",
    "                    max_v = nums[i]\n",
    "                    idx = [i]\n",
    "                elif nums[i] == max_v:\n",
    "                    idx.append(i)\n",
    "            if len(idx) == 1:\n",
    "                return max_v\n",
    "            elif len(idx) == 0:\n",
    "                return -1\n",
    "            for i in idx:\n",
    "                nums[i] = 0\n",
    "            if max_v == 0:\n",
    "                return -1\n",
    "            max_v = 0\n",
    "            idx = []\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        from collections import Counter\n",
    "        count = Counter(nums)\n",
    "        res = []\n",
    "        for key, val in count.items():\n",
    "            if val == 1:\n",
    "                res.append(key)\n",
    "        if len(res) == 0: return -1\n",
    "        else: return max(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        num_dict = dict()\n",
    "        for num in nums:\n",
    "            if num in num_dict.keys():\n",
    "                num_dict[num] += 1\n",
    "            else:\n",
    "                num_dict[num] = 1\n",
    "            \n",
    "        res = []\n",
    "        print(num_dict)\n",
    "        for k in num_dict.keys():\n",
    "            if num_dict[k] == 1:\n",
    "                res.append(k)\n",
    "        if len(res) == 0:\n",
    "            return -1\n",
    "        return max(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        dic = {}\n",
    "        list = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] in dic:\n",
    "                if nums[i] in list:\n",
    "                    list.remove(nums[i])\n",
    "                else: continue\n",
    "            else:\n",
    "                dic[nums[i]] = i\n",
    "                list.append(nums[i])\n",
    "        if list == []: return -1\n",
    "        return max(list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "      cnt=Counter(nums)\n",
    "      nums=sorted(cnt.items(),key=lambda x:-x[0])\n",
    "      for key,value in nums:\n",
    "        if value==1:\n",
    "          return key\n",
    "      else:\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestUniqueNumber(self, nums: List[int]) -> int:\n",
    "        counter = collections.Counter(nums)\n",
    "        counter_list = []\n",
    "        for num, count in counter.items():\n",
    "            counter_list.append((num, count))\n",
    "        counter_list = sorted(counter_list, key=lambda item: -item[0])\n",
    "        for item in counter_list:\n",
    "            if item[1] <= 1:\n",
    "                return item[0]\n",
    "        return -1\n",
    "            "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
